/*
* 重写uni.setStorageSync方法，可以设置过期时间
* expire默认为7天，传参时单位为"天"
* 设置时，单位转换为秒
*/
export const setStorageExpire = (name, value, expire = 7) => {
	let obj = {
		data: value,
		time: Date.now(),
		expire: expire * (24 * 60 * 60 * 1000)
	};
	uni.setStorage({
		key: name,
		data: JSON.stringify(obj)
	})
}
/*
* 获取uni.getStorageSync
* 过期后，设置为null
*/
export const getStorageExpire = (name) => {
	let val = uni.getStorageSync(name);
	if (!val) {
		return val
	}
	val = JSON.parse(val);
	if (Date.now() - val.time > val.expire) {
		uni.removeStorageSync(name);
		return null
	}
	return val.data
}

// 验证邮箱
export const validateEmail = (email) => {
	if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
		return true
	}
	return false
}

// 验证手机号
export const validatePhone = (phone) => {
	if (/^1[3-9][0-9]{9}$/.test(phone)) {
		return true
	}
	return false
}

// 验证登录
export const isLogin = () => {
	const access_token = getStorageExpire("access_token");
	if (access_token) {
		return true
	} 
	return false;
}